#include "vuser.h"
#include "vdatabase.h"
#include <QSqlQuery>

VUsers::VUsers(const QString& account, const QString password) 
	: m_account(account)
	, m_password(password)
{}

VUsers::VUsers(const VUsers& other)
	: m_account(other.m_account)
	, m_password(other.m_password)
{}

QString VUsers::account() const
{
	return m_account;
}

// 查询好友
QList<QString> VUsers::getFriends()
{
	QList<QString> friends;
	QSqlQuery query(VDataBase::ContactDatabase());
	query.exec(QString("SELECT FRIEND_ACCOUNT FROM '%1';").arg(m_account + "_FRIEND")); 
	while (query.next()) 
	{
		friends.append(query.value("FRIEND_ACCOUNT").toString());
	}
	return friends;
}

// 删除好友
void VUsers::removeFriend(const QString& friendAccount)
{
	QSqlQuery(VDataBase::ContactDatabase()).exec(QString("DELETE FROM '%1' WHERE FRIEND_ACCOUNT='%2';")
		.arg(m_account + "_FRIEND").arg(friendAccount));	
}

void VUsers::deleteAccount()
{
	QSqlQuery(VDataBase::AccountDatabase()).exec(QString("DELETE FROM USERS WHERE ACCOUNT='%1';")
		.arg(m_account)); //删除账户
}

void VUsers::addFriend(const QString& friendAccount)
{
	QSqlQuery query(VDataBase::ContactDatabase());
	query.exec(QString("INSERT OR IGNORE INTO '%1' (FRIEND_ACCOUNT) VALUES ('%2');")
		.arg(m_account + "_FRIEND").arg(friendAccount));
}